package cn.djun.news.service.impl;

import cn.djun.news.dao.CommentDao;
import cn.djun.news.entity.Comment;
import cn.djun.news.service.CommentService;
import cn.djun.news.util.Paginating;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author djun
 * @classname CommentServiceImpl
 * @description
 * @date 2020/12/7 15:43
 * @email 3170495286@qq.com
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    /**
     * 添加评论
     *
     * @param comment
     */
    @Override
    public int addComment(Comment comment) {
        try {
            return commentDao.insertComment(comment);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Paginating<Comment> getcommentbynewsid(Comment comment) {
        try {
            PageHelper.startPage(comment.getPageNum(), comment.getPageSize());
            List<Comment> list = commentDao.getCommentBynewsid(comment);
            PageInfo<Comment> p = new PageInfo<>(list);
            return new Paginating<>(p);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据新闻id查询评论数量
     * @param id
     * @return
     */
    @Override
    public int getCommentCountbynewid(int id) {
        try {
            int comment = commentDao.getCommentCountbynewid(id);
            return comment;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int delcommentbyid(Integer id) {
        try {
            return commentDao.delcommentbyid(id);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
